<?php
/**
 * Created by PhpStorm.
 * User: x
 * Date: 2018/11/20
 * Time: 16:47
 */

namespace app\index\controller;
use app\common\controller\HomeBase;
use think\Db;
use think\Config;
// aliyun oss
use OSS\OssClient;
use OSS\Core\OssException;
use app\common\model\AuthorWxUser as AuthorWxUserModel;

class Adminmsg extends HomeBase
{
    protected $oss_clinet;
    protected $access_key_id;
    protected $access_key_secret;
    protected $endpoint ;  //内网上传
    protected $endpoint_we;
    protected $oss_bucket;
    protected $security_token;
    protected $request_core;
    protected $oss_get_resurce;
    protected $oss_show_resource;
    protected $time_out;
    //
    protected $author_wx_user_model;

    /*
     * 初始化操作
     */
    public function _initialize() {
        $this->access_key_id =  Config::get('access_key_id') ;
        $this->access_key_secret = Config::get('access_key_secret') ;
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $this->endpoint = Config::get('weendpoint') ;
        $this->endpoint_we = Config::get('weendpoint2');
        $this->oss_clinet = new OssClient($this->access_key_id,$this->access_key_secret,$this->endpoint);
        $this->oss_bucket = Config::get('hc_oss_bucket') ;
        $this->time_out = Config::get('time_out');
        $this->oss_get_resurce = new OssClient($this->access_key_id,$this->access_key_secret,$this->endpoint,false,$this->security_token); //内网上传
        $this->oss_show_resource = new OssClient($this->access_key_id,$this->access_key_secret,$this->endpoint_we,false,$this->security_token);  //外网下载
        $this->author_wx_user_model = new AuthorWxUserModel();
    }

    //批量删除公众号已经不存在的资源文件
    public function deletewxs($page=1,$sqlid='',$keyword = '',$type='image',$nums=100){
                    if($sqlid == ''){
                        $sqlname = 'chatlog';
                    }else{
                        $sqlname = 'chatlog'.$sqlid;
                    }
//            var_dump($sqlname);
            $where = [];
            if($type){
                $where['type'] = $type;
                $where['content'] = ['like', "_uploads%"];
            }

        $appids = ['wx10a581413081a0d3',
            'wxdce09ad7ceab8ae8',
            'wx28aea4b2268f860b',
            'wx54af2793cf0857f6',
            'wx3c2844185d7f77d0',
            'wx0a4297a90f62b4dd',
            'wxb26e6dbea1a9e47c',
            'wx1189bdad91fc5a68',
            'wx99049e00fc07677c',
            'wxc5ed0d14043402fa',
            'wx80a685bddd49eac1',
            'wxe312e3befda6c59e',
            'wx26c25bdad791a63b',
            'wxc307ccb6f90f2bd9',
            'wx8e7998385d279764',
            'wx38e2cc6b78a24a2f',
            'wxa0c3b685bbe5dfd9',
            'wx9ea045d437226621',
            'wx2b59287ced46e884',
            'wxea95c4aecc488a0c',
            'wx6bdd5c698cc1920d',
            'wxe44233fd2eca626e',
            'wx523069982e20ed35',
            'wxd932c8629fa30d5f',
            'wx9d6292280615552c',
            'wx972be8af469b3385',
            'wxbca65fafa900e7ef'];

            //        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
            //            ->order('send_time asc')->paginate(15,false,['page'=>$page]);
            $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
                ->order('send_time asc')->paginate($nums,false,['query'=>['page'=>$page,'sqlid'=>$sqlid,'nums'=>$nums]]);

        foreach ($sql_data as $img_data) {

        }


    }

    public function index2($page=1,$sqlid='',$keyword = '',$type='image',$nums=15){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        var_dump($sqlname);
        $where = [];
        if($type){
            $where['type'] = $type;
            $where['content'] = ['like', "_uploads%"];
        }
//        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
//            ->order('send_time asc')->paginate(15,false,['page'=>$page]);
        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
            ->order('send_time asc')->paginate($nums,false,['query'=>['page'=>$page,'sqlid'=>$sqlid,'nums'=>$nums]]);
        foreach ($sql_data as $img_data) {
            if(empty($img_data['oss_name'])){
                $img_data['oss_name'] = 'http://img05.tooopen.com/images/20141217/sy_77493739982.jpg';
            }else{
                if(empty($img_data['oss_url'])){
                    try{
                        $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                        if($signeurl){
                            // 处理oss内网地址, 修改成外网地址
                            str_replace($this->endpoint,$this->endpoint_we,$signeurl);

                            $oss_data = [
                                'oss_url' =>$signeurl,
                                'oss_time' => time() + $this->time_out
                            ];
                            Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                        }
                    }catch (OssException $e){
                        array_push($result,$img_data['id']);
                    }
                }else{
                    if($img_data['oss_time'] < time()){
                        try{
                            $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                            if($signeurl){
                                str_replace($this->endpoint,$this->endpoint_we,$signeurl);
                                $oss_data = [
                                    'oss_url' =>$signeurl,
                                    'oss_time' => time() + $this->time_out
                                ];
                                Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                            }
                        }catch (OssException $e){
                            array_push($result,$img_data['id']);
                        }
                    }
                }
            }
        }

        return $this->fetch('index2',[
            'msg_list' => $sql_data,
            'keyword'=>  $keyword ,
            'type'=>  $type  ,
            'sqlid' => $sqlid,'nums'=>$nums
        ]);
    }
    public function index3($page=1,$sqlid='',$keyword = '',$type='image',$nums=15,$islist=1){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        var_dump($sqlname);
        $where = [];
        if($type){
            $where['type'] = $type;
            $where['content'] = ['like', "_uploads%"];
        }

        $appids = ['wx10a581413081a0d3',
            'wxdce09ad7ceab8ae8',
            'wx28aea4b2268f860b',
            'wx54af2793cf0857f6',
            'wx3c2844185d7f77d0',
            'wx0a4297a90f62b4dd',
            'wxb26e6dbea1a9e47c',
            'wx1189bdad91fc5a68',
            'wx99049e00fc07677c',
            'wxc5ed0d14043402fa',
            'wx80a685bddd49eac1',
            'wxe312e3befda6c59e',
            'wx26c25bdad791a63b',
            'wxc307ccb6f90f2bd9',
            'wx8e7998385d279764',
            'wx38e2cc6b78a24a2f',
            'wxa0c3b685bbe5dfd9',
            'wx9ea045d437226621',
            'wx2b59287ced46e884',
            'wxea95c4aecc488a0c',
            'wx6bdd5c698cc1920d',
            'wxe44233fd2eca626e',
            'wx523069982e20ed35',
            'wxd932c8629fa30d5f',
            'wx9d6292280615552c',
            'wx972be8af469b3385',
            'wxbca65fafa900e7ef'];
        $show_array = [];

//        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
//            ->order('send_time asc')->paginate(15,false,['page'=>$page]);
        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
            ->order('send_time asc')->paginate($nums,false,['query'=>['page'=>$page,'sqlid'=>$sqlid,'nums'=>$nums,'islist'=>$islist]]);
        foreach ($sql_data as $img_data) {
            // 查看appid在不在列表里面
            if($islist  == 2){
                // 在列表里面
                if(in_array($img_data['appid'],$appids)){

//                    $img_data['oss_name'] = '待上传';
                    if(empty($img_data['oss_name'])){
                        $img_data['oss_name'] = '待上传';
                    }else{
                        if(empty($img_data['oss_url'])){
                            try{
                                $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                                if($signeurl){
                                    // 处理oss内网地址, 修改成外网地址
                                    str_replace($this->endpoint,$this->endpoint_we,$signeurl);

                                    $oss_data = [
                                        'oss_url' =>$signeurl,
                                        'oss_time' => time() + $this->time_out
                                    ];
                                    Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                                }
                                $img_data['oss_url'] = $signeurl;

                            }catch (OssException $e){
                                array_push($result,$img_data['id']);
                            }
                        }else{
                            if($img_data['oss_time'] < time()){
                                try{
                                    $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                                    if($signeurl){
                                        str_replace($this->endpoint,$this->endpoint_we,$signeurl);
                                        $oss_data = [
                                            'oss_url' =>$signeurl,
                                            'oss_time' => time() + $this->time_out
                                        ];
                                        Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                                    }
                                    $img_data['oss_url'] = $signeurl;
                                }catch (OssException $e){
                                    array_push($result,$img_data['id']);
                                }
                            }
                        }
                    }
                    array_push($show_array,$img_data);

                }
            }else{
                // 显示不在列表里面的数据
                if(! in_array($img_data['appid'],$appids)){
                    $img_data['oss_name'] = '待删除';
                    array_push($show_array,$img_data);
                }
            }






        }
//        return json([
//            'msg_list' => $show_array,
//            'pages' => $sql_data,
//            'keyword'=>  $keyword ,
//            'type'=>  $type  ,
//            'sqlid' => $sqlid,'nums'=>$nums,'islist'=>$islist
//        ]);

        return $this->fetch('index3',[
            'msg_list' => $show_array,
            'pages' => $sql_data,
            'keyword'=>  $keyword ,
            'type'=>  $type  ,
            'sqlid' => $sqlid,'nums'=>$nums,'islist'=>$islist
        ]);
    }

    public function indext($page=1,$sqlid='',$keyword = '',$type='image',$nums=15,$islist=1){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        var_dump($sqlname);
        $where = [];
        if($type){
            $where['type'] = $type;
            $where['content'] = ['like', "_uploads%"];
        }

        $appids = ['wx10a581413081a0d3',
            'wxdce09ad7ceab8ae8',
            'wx28aea4b2268f860b',
            'wx54af2793cf0857f6',
            'wx3c2844185d7f77d0',
            'wx0a4297a90f62b4dd',
            'wxb26e6dbea1a9e47c',
            'wx1189bdad91fc5a68',
            'wx99049e00fc07677c',
            'wxc5ed0d14043402fa',
            'wx80a685bddd49eac1',
            'wxe312e3befda6c59e',
            'wx26c25bdad791a63b',
            'wxc307ccb6f90f2bd9',
            'wx8e7998385d279764',
            'wx38e2cc6b78a24a2f',
            'wxa0c3b685bbe5dfd9',
            'wx9ea045d437226621',
            'wx2b59287ced46e884',
            'wxea95c4aecc488a0c',
            'wx6bdd5c698cc1920d',
            'wxe44233fd2eca626e',
            'wx523069982e20ed35',
            'wxd932c8629fa30d5f',
            'wx9d6292280615552c',
            'wx972be8af469b3385',
            'wxbca65fafa900e7ef'];
        $show_array = [];

//        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
//            ->order('send_time asc')->paginate(15,false,['page'=>$page]);
        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
            ->order('send_time asc')->paginate($nums,false,['query'=>['page'=>$page,'sqlid'=>$sqlid,'nums'=>$nums,'islist'=>$islist]]);
        foreach ($sql_data as $img_data) {
            // 查看appid在不在列表里面
            if($islist  == 2){
                // 在列表里面
                if(in_array($img_data['appid'],$appids)){

//                    $img_data['oss_name'] = '待上传';
                    if(empty($img_data['oss_name'])){
                        $img_data['oss_name'] = '待上传';
                    }else{
                        if(empty($img_data['oss_url'])){
                            try{
                                $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                                if($signeurl){
                                    // 处理oss内网地址, 修改成外网地址
                                    str_replace($this->endpoint,$this->endpoint_we,$signeurl);

                                    $oss_data = [
                                        'oss_url' =>$signeurl,
                                        'oss_time' => time() + $this->time_out
                                    ];
                                    Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                                }
                                $img_data['oss_url'] = $signeurl;

                            }catch (OssException $e){
                                array_push($result,$img_data['id']);
                            }
                        }else{
                            if($img_data['oss_time'] < time()){
                                try{
                                    $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                                    if($signeurl){
                                        str_replace($this->endpoint,$this->endpoint_we,$signeurl);
                                        $oss_data = [
                                            'oss_url' =>$signeurl,
                                            'oss_time' => time() + $this->time_out
                                        ];
                                        Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                                    }
                                    $img_data['oss_url'] = $signeurl;
                                }catch (OssException $e){
                                    array_push($result,$img_data['id']);
                                }
                            }
                        }
                    }
                    array_push($show_array,$img_data);

                }
            }else{
                // 显示不在列表里面的数据
                if(! in_array($img_data['appid'],$appids)){
                    $img_data['oss_name'] = '待删除';
                    array_push($show_array,$img_data);
                }
            }


        }
        return json([
            'msg_list' => $show_array,
            'pages' => $sql_data,
            'keyword'=>  $keyword ,
            'type'=>  $type  ,
            'sqlid' => $sqlid,'nums'=>$nums,'islist'=>$islist,
            'current_page' => $page,
        ]);

//        return $this->fetch('index3',[
//            'msg_list' => $show_array,
//            'pages' => $sql_data,
//            'keyword'=>  $keyword ,
//            'type'=>  $type  ,
//            'sqlid' => $sqlid,'nums'=>$nums,'islist'=>$islist
//        ]);
    }

    // 自动筛选当前公众号还绑定的数据
    public  function index4($page=1,$sqlid='',$keyword = '',$type='image',$nums=15){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        $where = [];
        $appids = ['wx10a581413081a0d3',
            'wxdce09ad7ceab8ae8',
            'wx28aea4b2268f860b',
            'wx54af2793cf0857f6',
            'wx3c2844185d7f77d0',
            'wx0a4297a90f62b4dd',
            'wxb26e6dbea1a9e47c',
            'wx1189bdad91fc5a68',
            'wx99049e00fc07677c',
            'wxc5ed0d14043402fa',
            'wx80a685bddd49eac1',
            'wxe312e3befda6c59e',
            'wx26c25bdad791a63b',
            'wxc307ccb6f90f2bd9',
            'wx8e7998385d279764',
            'wx38e2cc6b78a24a2f',
            'wxa0c3b685bbe5dfd9',
            'wx9ea045d437226621',
            'wx2b59287ced46e884',
            'wxea95c4aecc488a0c',
            'wx6bdd5c698cc1920d',
            'wxe44233fd2eca626e',
            'wx523069982e20ed35',
            'wxd932c8629fa30d5f',
            'wx9d6292280615552c',
            'wx972be8af469b3385',
            'wxbca65fafa900e7ef'];
        if($type){
            $where['type'] = $type;
            $where['content'] = ['like', "_uploads%"];
            $where['appid'] = ['IN', $appids];
        }
       
        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
        ->order('send_time asc')
        ->paginate($nums,false,['query'=>['page'=>$page,'sqlid'=>$sqlid,'nums'=>$nums]]);
        // 获取对应的数组数据,然后进行上传到oss
        
        
        foreach ($sql_data as $img_data) {
            if(empty($img_data['oss_name'])){
                $img_data['oss_name'] = 'http://img05.tooopen.com/images/20141217/sy_77493739982.jpg';
            }else{
                if(empty($img_data['oss_url'])){
                    try{
                        $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                        if($signeurl){
                            // 处理oss内网地址, 修改成外网地址
                            str_replace($this->endpoint,$this->endpoint_we,$signeurl);
                            
                            $oss_data = [
                                'oss_url' =>$signeurl,
                                'oss_time' => time() + $this->time_out
                            ];
                            Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                        }
                    }catch (OssException $e){
                        array_push($result,$img_data['id']);
                    }
                }else{
                    if($img_data['oss_time'] < time()){
                        try{
                            $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                            if($signeurl){
                                str_replace($this->endpoint,$this->endpoint_we,$signeurl);
                                $oss_data = [
                                    'oss_url' =>$signeurl,
                                    'oss_time' => time() + $this->time_out
                                ];
                                Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                            }
                        }catch (OssException $e){
                            array_push($result,$img_data['id']);
                        }
                    }
                }
                
            }
        }
        
        return $this->fetch('index',[
            'msg_list' => $sql_data,
            'keyword'=>  $keyword ,
            'type'=>  $type  ,
            'sqlid' => $sqlid
        ]);
        
    }
    
    public  function auto_upload_oss($page=1,$sqlid='',$keyword = '',$type='image',$nums=100){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        $where = [];
        $appids = ['wx10a581413081a0d3',
            'wxdce09ad7ceab8ae8',
            'wx28aea4b2268f860b',
            'wx54af2793cf0857f6',
            'wx3c2844185d7f77d0',
            'wx0a4297a90f62b4dd',
            'wxb26e6dbea1a9e47c',
            'wx1189bdad91fc5a68',
            'wx99049e00fc07677c',
            'wxc5ed0d14043402fa',
            'wx80a685bddd49eac1',
            'wxe312e3befda6c59e',
            'wx26c25bdad791a63b',
            'wxc307ccb6f90f2bd9',
            'wx8e7998385d279764',
            'wx38e2cc6b78a24a2f',
            'wxa0c3b685bbe5dfd9',
            'wx9ea045d437226621',
            'wx2b59287ced46e884',
            'wxea95c4aecc488a0c',
            'wx6bdd5c698cc1920d',
            'wxe44233fd2eca626e',
            'wx523069982e20ed35',
            'wxd932c8629fa30d5f',
            'wx9d6292280615552c',
            'wx972be8af469b3385',
            'wxbca65fafa900e7ef'];
        if($type){
            $where['type'] = $type;
            $where['content'] = ['like', "_uploads%"];
            $where['appid'] = ['IN', $appids];
        }
        
        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
        ->order('send_time asc')
        ->paginate($nums,false,['query'=>['page'=>$page,'sqlid'=>$sqlid,'nums'=>$nums]]);
        // 获取对应的数组数据,然后进行上传到oss
        $oss_result = []; $oss_num = 0;$has_upload=0;$oss_error=0;
        foreach ($sql_data as $img_data){
            if(empty($img_data['oss_name'])){
                $object_name = substr($img_data['content'],1);
                $localPath = PUBLIC_PATH;
                $object_path = $localPath.$img_data['content'];
                // 上传到OSS
                try{
                    $upload_oss = $this->oss_clinet->uploadFile($this->oss_bucket,$object_name,$object_path);
                    if($upload_oss){
                        // 写入数据库
                        $oss_data = [ 'oss_name' => $object_name,];
                        // $sql_data = Db::connect('db_config_chatlog')->name($sqlname)
                        Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                        array_push($oss_result,$img_data['id']);
                        $oss_num += 1;
                    }
                }catch (OssException $e){
                    array_push($oss_result,$img_data['id']);
                    $oss_error += 1;
//                     var_dump($e);
                }
            }else{
                $has_upload += 1;
            }   
        }

        return  json([
            'status' => 200,
            'successful' => '上传成功'.$oss_num,
            'has_upload' => '已上传'.$has_upload,
            'oss_error' => '失败'.$oss_error,
            'current_page' => $page,
            'sql_data' => $sql_data
        ]); 
    }
    
    // 新版显示oss图片资源
    public function index($page=1,$sqlid='',$keyword = '',$type='image'){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        var_dump($sqlname);
        $where = [];
        if($type){
            $where['type'] = ['like', "%{$type}%"];
        }
        if (!empty($keyword)) {
            $where['content'] = ['like', "%{$keyword}%"];
        }
//        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
//            ->order('send_time asc')->paginate(15,false,['page'=>$page]);
        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
            ->order('send_time asc')->paginate(15,false,['query'=>['page'=>$page,'sqlid'=>$sqlid]]);
        foreach ($sql_data as $img_data) {
            if(empty($img_data['oss_name'])){
                $img_data['oss_name'] = 'http://img05.tooopen.com/images/20141217/sy_77493739982.jpg';
            }else{
                if(empty($img_data['oss_url'])){
                    try{
                        $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                        if($signeurl){
                            // 处理oss内网地址, 修改成外网地址
                            str_replace($this->endpoint,$this->endpoint_we,$signeurl);

                            $oss_data = [
                                'oss_url' =>$signeurl,
                                'oss_time' => time() + $this->time_out
                            ];
                            Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                        }
                    }catch (OssException $e){
                        array_push($result,$img_data['id']);
                    }
                }else{
                    if($img_data['oss_time'] < time()){
                        try{
                            $signeurl = $this->oss_show_resource->signUrl($this->oss_bucket,$img_data['oss_name'] ,$this->time_out);
                            if($signeurl){
                                str_replace($this->endpoint,$this->endpoint_we,$signeurl);
                                $oss_data = [
                                    'oss_url' =>$signeurl,
                                    'oss_time' => time() + $this->time_out
                                ];
                                Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                            }
                        }catch (OssException $e){
                            array_push($result,$img_data['id']);
                        }
                    }
                }

            }
        }

        return $this->fetch('index',[
            'msg_list' => $sql_data,
            'keyword'=>  $keyword ,
            'type'=>  $type  ,
            'sqlid' => $sqlid
        ]);
    }

    /*  ----------------------------------  写入OSS 服务器  ----------------------------------
     * 本地写入oss 批量写入
     * */
    public function local_save_oss($id = 0, $ids = [],$sqlid=''){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        $id = $ids ? $ids : $id ;
        if($id) {
            $where['id'] = ['IN', $ids];
            $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)->select();
            $oss_result = []; $oss_num = 0;$has_upload=0;$oss_error=0;
            foreach ($sql_data as $img_data) {
                if(empty($img_data['oss_name'])){
                    $object_name = substr($img_data['content'],1);
                    $localPath = PUBLIC_PATH;
                    $object_path = $localPath.$img_data['content'];
                    // 上传到OSS
                    try{
                        $upload_oss = $this->oss_clinet->uploadFile($this->oss_bucket,$object_name,$object_path);
                        if($upload_oss){
                            // 写入数据库
                            $oss_data = [ 'oss_name' => $object_name,];
                            // $sql_data = Db::connect('db_config_chatlog')->name($sqlname)
                            Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
                            array_push($oss_result,$img_data['id']);
                            $oss_num += 1;
                        }
                    }catch (OssException $e){
                        array_push($oss_result,$img_data['id']);
                        $oss_error += 1;
                        var_dump($e);
                    }
                }else{
                    $has_upload += 1;
                }

            }
            // 显示上传成功的个数
            $this->success('上传成功'.$oss_num.'个,已上传'.$has_upload.'个,失败'.$oss_error);
        }else{
            $this->error('请选择需要上传的图片');
        }
    }

    // 写入一个文件
    public function local_save_one($id,$sqlid=''){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        if($id) {
            $img_data = Db::connect('db_config_chatlog')->name($sqlname)->where(['id'=> $id])->find();
//            var_dump($img_data);
            $object_name = substr($img_data['content'],1);
//            var_dump($object_name);
            $localPath = PUBLIC_PATH;
            $object_path = $localPath.$img_data['content'];
            if(file_exists($object_path)){
                $result = [];
                // 上传到OSS
                try{
                    $upload_oss = $this->oss_clinet->uploadFile($this->oss_bucket,$object_name,$object_path);
                    if($upload_oss){
                        $result['status'] = 1;
                        // 写入数据库
                        $oss_data = [
                            'oss_name' => $object_name,
                        ];
                        $successful =  Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
//                        return json($successful);
                    $this->success('上传成功');
                    }
                }catch (OssException $e){
//                var_dump($e);
//                    return json($e);
//                $result['status'] = 404;
                $this->error('上传失败');
                }
            }else{
                $this->error('文件不存在');
            }
        }else{
            $this->error('非法请求');
        }
    }

    public function local_save_onet($id,$sqlid=''){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        if($id) {
            $img_data = Db::connect('db_config_chatlog')->name($sqlname)->where(['id'=> $id])->find();
//            var_dump($img_data);
            $object_name = substr($img_data['content'],1);
//            var_dump($object_name);
            $localPath = PUBLIC_PATH;
            $object_path = $localPath.$img_data['content'];
            var_dump($object_path);
//            if(file_exists($object_path)){
//                $result = [];
//                // 上传到OSS
//                try{
//                    $upload_oss = $this->oss_clinet->uploadFile($this->oss_bucket,$object_name,$object_path);
//                    if($upload_oss){
//                        $result['status'] = 1;
//                        // 写入数据库
//                        $oss_data = [
//                            'oss_name' => $object_name,
//                        ];
//                        $successful =  Db::connect('db_config_chatlog')->name($sqlname)->where('id',$img_data['id'])->update($oss_data);
////                    var_dump($successful);
//                        return json($successful);
////                    $this->success('上传成功');
//                    }
//                }catch (OssException $e){
////                var_dump($e);
//                    return json($e);
////                $result['status'] = 404;
////                $this->error('上传失败');
//                }
//            }else{
//                $this->error('文件不存在');
//            }
        }else{
            $this->error('非法请求');
        }
    }

    public function delete_local($id,$sqlid=''){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        $img_data = Db::connect('db_config_chatlog')->name($sqlname)->where(['id'=> $id])->find();
//        var_dump($img_data);
        $localPath = PUBLIC_PATH;
        $object_path = $localPath.$img_data['content'];
//        var_dump($object_path);
        // 校验文件是否存在
        if(file_exists($object_path)){
            if(unlink($object_path)){
                $this->success('成功'.$id);
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->success('文件不存在'.$id);
        }

    }

    public function delete_local_one($id,$sqlname=''){
        $img_data = Db::connect('db_config_chatlog')->name($sqlname)->where(['id'=> $id])->find();
//        var_dump($img_data);
        $localPath = PUBLIC_PATH;
        $object_path = $localPath.$img_data['content'];
//        var_dump($object_path);
        if(unlink($object_path)){
//            $this->success('成功'.$id);
            return $id;
        }else{
//            $this->error('删除失败');
            return false;
        }
    }


    public function delete_local_array($id = 0, $ids = [],$sqlid=''){
        $id = $ids ? $ids : $id ;
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        $num = 0;
        if($id){
            foreach ($ids as $oid) {
              $id_result =  $this->delete_local_one($oid,$sqlname);
                if($id_result){
                    $num += 1;
                }
            }
            $this->success('删除'.$num.'个');
        }else{
            $this->error('请选择需要删除的图片');
        }
    }



    // 删除单个本地的资源文件
    public function delete_local1($id,$sqlid=''){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        $img_data = Db::connect('db_config_chatlog')->name($sqlname)->where(['id'=> $id])->find();
        var_dump($img_data);
        $localPath = PUBLIC_PATH;
        $object_path = $localPath.$img_data['content'];
        var_dump($object_path);
//        if(unlink($object_path)){
//            $this->success('成功'.$id);
//        }else{
//            $this->error('删除失败');
//        }
    }



    // 删除本地多个文件
    public function delete_locals($id = 0, $ids = [],$sqlid=''){
        $id = $ids ? $ids : $id ;
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        if($id){
            $where['id'] = ['IN',$ids];
            $img_datas = Db::connect('db_config_chatlog')->name($sqlname)->where($where)->select();
            $delete_error = 0; $delete_num = 0;$no_file=0;
            $localPath = PUBLIC_PATH;
            foreach ($img_datas as $img_data) {
                $img_path = $localPath.$img_data['content'];
                if(file_exists($img_path)){
                    if(unlink($img_path)){
                        $delete_num += 1;
                    }else{
                        $delete_error += 1;
                    }
                }else{
                    $no_file += 1;
                }
            }
            $this->success('删除'.$delete_num.'个,失败'.$delete_error.'不存在'.$no_file);
        }else{
            $this->error('请选择需要删除的图片');
        }

    }

    public function index_old($page=1,$sqlid='',$keyword = '',$type='image'){
        if($sqlid == ''){
            $sqlname = 'chatlog';
        }else{
            $sqlname = 'chatlog'.$sqlid;
        }
        var_dump($sqlname);
        $where = [];
        if($type){
            $where['type'] = ['like', "%{$type}%"];
        }
        if (!empty($keyword)) {
            $map['content'] = ['like', "%{$keyword}%"];
        }
        $sql_data = Db::connect('db_config_chatlog')->name($sqlname)->where($where)
            ->order('send_time asc')->paginate(15,false,['page'=>$page]);

        return $this->fetch('index',[
            'msg_list' => $sql_data,
            'keyword'=>  $keyword ,
            'type'=>  $type  ,
            'sqlid' => $sqlid
        ]);
    }

    /*
     *  测试config文件调用
     * */
    public function show_id(){
        $access_key_id = ('access_key_id') ;
        echo $access_key_id;
        $config_id = Config::get('access_key_id');
        echo $config_id;
    }
    public function showurl(){
        $access_key_id = ('weendpoint2') ;
        echo $access_key_id;
    }

}